package com.example.administrator.szb.fragments.search;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.WebViewActivity;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.adapter.BaseAdapter;
import com.example.administrator.szb.bean.SearchBean;
import com.example.administrator.szb.fragments.base.MVPBaseFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by stone on 2018/1/12.
 */

public class SearchZX extends MVPBaseFragment {
    View ll_nodata;
    RecyclerView recyclerView;
    BaseAdapter<SearchBean.DataBean.ArtiListBean> adapter_ArtiListBean;//文章
    ArrayList<SearchBean.DataBean.ArtiListBean> mdata_artiListBean = new ArrayList<>();
    private TextView top_tips;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_searchs,null);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView(View view) {
        top_tips = view.findViewById(R.id.top_tips);

        recyclerView = view.findViewById(R.id.recyclerView2);
        recyclerView.setVisibility(View.VISIBLE);
        ListView listView = view.findViewById(R.id.recyclerView);
        listView.setVisibility(View.GONE);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        ll_nodata = view.findViewById(R.id.ll_nodata);
        isLoaded = true;
    }
    private boolean isLoaded = false;
    private boolean isfirst = true;
    @Override
    protected void initEvent() {
        if (!isLoaded){
            return;
        }
        if (mdata_artiListBean.size()==0){
            if (!isfirst){
                recyclerView.setVisibility(View.GONE);
                ll_nodata.setVisibility(View.VISIBLE);
            }

        }else {
            ll_nodata.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
        }

        top_tips.setVisibility(View.VISIBLE);
        top_tips.setText("共搜索到"+mdata_artiListBean.size()+"条相关投行资讯");
        if (adapter_ArtiListBean==null){
            adapter_ArtiListBean = new BaseAdapter<SearchBean.DataBean.ArtiListBean>(getActivity(),mdata_artiListBean) {
                @Override
                public void convert(int position, BaseViewHolder holder, SearchBean.DataBean.ArtiListBean listBean) {
                    ((TextView)holder.getView(R.id.title)).setText(listBean.getTitle());
                    ((TextView)holder.getView(R.id.time)).setText(listBean.getCreate_time());
                    ((ImageView)holder.getView(R.id.head_img)).setVisibility(View.GONE);
                    ((TextView)holder.getView(R.id.liulan)).setText(listBean.getReading_num()+"人浏览");
                    holder.getView(R.id.lines).setVisibility(View.VISIBLE);
                    if (TextUtils.isEmpty(listBean.getImage())) {
                        //(holder.getView(R.id.imageView23)).setVisibility(View.GONE);
                    }else{
                        Glide.with(getActivity()).load(listBean.getImage()).placeholder(R.mipmap.defaultimg).into(((ImageView)holder.getView(R.id.head_img)));
                        ((ImageView)holder.getView(R.id.head_img)).setVisibility(View.VISIBLE);
                    }
                }

                @Override
                public int getLayoutId(int viewType) {
                    return R.layout.item_shocang_zx;
                }
            };
            adapter_ArtiListBean.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Intent intent = new Intent(getActivity(), WebViewActivity.class);
                    intent.putExtra("wz_id",mdata_artiListBean.get(position).getId());
                    intent.putExtra("title",mdata_artiListBean.get(position).getTitle());
                    intent.putExtra("image",mdata_artiListBean.get(position).getImage());
                    startActivity(intent);
                }
            });
            recyclerView.setAdapter(adapter_ArtiListBean);
        }else {
            adapter_ArtiListBean.notifyDataSetChanged();
        }

        isfirst = false;
    }

    @Override
    protected void initData() {

    }
    public void setDatas(List list){
        if (list==null){
            list = new ArrayList();
        }
        mdata_artiListBean.clear();
        mdata_artiListBean.addAll(list);
        initEvent();
    }
}
